# -*- coding: utf-8 -*- 


from django.core.cache import cache
from django.contrib.auth.forms import UserCreationForm
from django.views.generic.simple import direct_to_template
from django.http import HttpResponseRedirect
from django.views.generic.simple import direct_to_template
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from nekoTrace.nekonekotrace.logic import RenrenAPI
from nekoTrace.nekonekotrace.logic import Core
import datetime

def mainpageView(request):
    #if (request.Post[""])    
    return direct_to_template(request, '../templates/index.html')

def testpageView(request):
    #if (request.Post[""])    
    return direct_to_template(request, '../templates/test/post_test.html')

def welcomeView(request):
    """
        here record the sig attributes
    """
    for key in request.GET.keys():
        request.session[key] = request.GET[key]
        
    return direct_to_template(request, "../templates/RenRenAPP/welcome.html")

def homeView(request):
#    # test get user info
#    params = {"method": "users.getInfo", "fields": "name,headurl,tinyurl"}
#    #params = {"method": "friends.getFriends", "fields": "headurl_with_logo,tinyurl_with_logo"}
#    apiClient = RenrenAPI.RenRenAPIClient(request.GET['xn_sig_session_key'])
#    response = apiClient.request(params)
#    response[0][u"name"].encode("utf-8")
#    print response[0][u"name"]
#    # response is a json format list
#    # list dict...
#    t = get_template('../templates/RenRenAPP/home.html')
#    html = t.render(Context({'userInfo' : response[0]}))
#    return HttpResponse(html)
    #test:    check if cur user is in DB, if no, add an item in DB. Then print UserInfo
    apiClient = RenrenAPI.RenRenAPIClient(request.GET['xn_sig_session_key'])
    Core.checkNewUser(request.GET['xn_sig_user'], apiClient)
    t = get_template('../templates/RenRenAPP/home.html')
    html = t.render(Context({'userInfo' : "SB"}))
    return HttpResponse(html)